package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.RelayCondition;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 获取继电器自动控制
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetPowerRelayAuto implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 发送设备序号
         */
        private Integer prcIndex;

        /**
         * 端口序号
         */
        private Integer portIndex;

        /**
         * 多功能卡序号
         */
        private Integer functionCardIndex;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 发送设备序号
         */
        private Integer prcIndex;

        /**
         * 端口序号
         */
        private Integer portIndex;

        /**
         * 自动控制条件
         */
        private List<RelayCondition> condition;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.GetPowerRelayAuto.InputMsg.Builder inputMsgBuilder =
                CompleteInstruction.GetPowerRelayAuto.InputMsg.newBuilder();
        GetPowerRelayAuto.InputMsg thisInput = this.getInputMsg();
        inputMsgBuilder.setPrcIndex(thisInput.getPrcIndex());
        inputMsgBuilder.setPortIndex(thisInput.getPortIndex());
        inputMsgBuilder.setFunctionCardIndex(thisInput.getFunctionCardIndex());

        final CompleteInstruction.GetPowerRelayAuto.Builder builder =
                CompleteInstruction.GetPowerRelayAuto.newBuilder();
        builder.setInput(inputMsgBuilder);
        return builder.build().toByteString();
    }

    @Override
    public GetPowerRelayAuto byteConvertObject(ByteString bytes) {
        CompleteInstruction.GetPowerRelayAuto getPowerRelayAuto = null;
        try {
            getPowerRelayAuto = CompleteInstruction.GetPowerRelayAuto.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(getPowerRelayAuto)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        GetPowerRelayAuto.OutputMsg outputMsg = new GetPowerRelayAuto.OutputMsg();
        CompleteInstruction.GetPowerRelayAuto.OutputMsg output = getPowerRelayAuto.getOutput();
        outputMsg.setPrcIndex(output.getPrcIndex());
        outputMsg.setPortIndex(output.getPortIndex());
        List<RelayCondition> collect = output.getConditionList().stream().map(e -> new RelayCondition().formProto(e)).collect(Collectors.toList());
        outputMsg.setCondition(collect);
        this.setOutputMsg(outputMsg);
        return this;
    }
}

